/**
 * 产品成本价格维护 - 编辑页面
 * @author liuw
 * @date 2019/09/04
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            form: '',
            rules: {
                GWER001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWER002: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWER003: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWER004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWERPLANT: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]
            },
            selectOptions: {
                sys_y_n: app.datas_("sys_y_n"),
                options1: [],
                options2: [],
                options3: []
            }
        },
        callObj,
        pageVue
    ;

    var Gwei023Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwei023_edit',
                _loadTplsFull: [{
                    dom: 'common_open_window', tpl: 'common/_open_window'
                }]
            }, win);
        },
        edit: function () {
            app.ajax({
                xurl: 'crm/gwei023/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: '产品成本价格信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        //callObj.update(result.xdata);
                        app.pageModule('gwei023').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },
        add: function () {
            app.ajax({
                xurl: 'crm/gwei023/add',
                xdata: vueData.form,
                loading: true,
                okmsg: '产品成本价格信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwei023').reloadTableCurr();
                        layer.close(callObj.layOpenIndex);
                    }
                }
            });
        },
        _call: function (obj) {
            callObj = obj;
            pageVue.$refs.form.resetFields();
            if (obj.is_add) {
                vueData.isAdd = true;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWERACTI: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['GWER']);
            pageVue = new Vue({
                el: '#gwei023_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(callObj.layOpenIndex);
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.add();
                                } else {
                                    self.edit();
                                }
                            }
                        });
                    },
                    change: function (val) {
                        var obj = {};
                        obj = vueData.selectOptions.options1.find(function (item) {
                            return item.GWAM001 === val
                        });
                        vueData.form.GWAML003 = obj.GWAML003;
                        vueData.form.GWAML004 = obj.GWAML004;
                    },
                    openCp:function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method:'q_gwam001',
                                    columnList: [
                                        {label: '产品编号', prop: 'GWAM001', width: '150'},
                                        {label: '产品名称', prop: 'GWAML003', width: '150'},
                                        {label: '规格', prop: 'GWAML004', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWER001 = data.GWAM001;
                                    vueData.form.GWAML003 = data.GWAML003;
                                    vueData.form.GWAML004 = data.GWAML004;
                                },
                            },
                            index: 0,
                            openOpts: {
                                title: '选择产品',
                                area: ['600px', '465px']
                            }
                        });
                    },
                    clearGWER001:function () {
                        if(vueData.form.GWER001 == '' || vueData.form.GWER001 == null){
                            vueData.form.GWAML003 = '';
                            vueData.form.GWAML004 = '';
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwei023_edit');
                }
            });
        },
        _lazy: function () {
            /*app.ajax({
                xurl: 'crm/gwei023/getGwamList',
                cb: function (result) {
                    vueData.selectOptions.options1 = result.data.list;
                }
            });*/
            app.ajax({
                xurl: 'crm/gwei023/getGwacList',
                cb: function (result) {
                    vueData.selectOptions.options2 = result.data.list;
                }
            });
            app.ajax({
                xurl: 'crm/gwei023/getGwabList',
                cb: function (result) {
                    vueData.selectOptions.options3 = result.data.list;
                }
            });
            this._super();
        }
    });

    return new Gwei023Edit();

});